package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Addperation implements IOPeration{

    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书!");
        System.out.println("请输入书名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入图书类型：");
        String type = scanner.nextLine();
        System.out.println("请输入图书价格：");
        double price = scanner.nextDouble();

        Book book = new Book(name,author,type,price);//创建该书
        int currentSize = bookList.getUsedSize();
        //判断书是否存在
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name))//判断是否存在
            {
                System.out.println("该书已存在 不能重复添加！");
                return;
            }
        }
        //找到尾巴位置 进行新增
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize+1);
        System.out.println("添加成功");
    }
}
